

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>packnet_sfm.models.model_wrapper &mdash; PackNet-SfM 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
        <script src="../../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/custom.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html">
          

          
            
            <img src="../../../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../configs/configs.html">Configs</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../scripts/scripts.html">Scripts</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../trainers/trainers.html">Trainers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../datasets/datasets.html">Datasets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../models/models.html">Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../networks/networks.html">Networks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../losses/losses.html">Losses</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../loggers/loggers.html">Loggers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../geometry/geometry.html">Geometry</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../utils/utils.html">Utils</a></li>
</ul>
<p class="caption"><span class="caption-text">Contact</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://tri.global">Toyota Research Institute</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/TRI-ML/packnet-sfm">PackNet-SfM GitHub</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/TRI-ML/DDAD">DDAD GitHub</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">PackNet-SfM</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>packnet_sfm.models.model_wrapper</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for packnet_sfm.models.model_wrapper</h1><div class="highlight"><pre>
<span></span><span class="c1"># Copyright 2020 Toyota Research Institute.  All rights reserved.</span>

<span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">OrderedDict</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">random</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">from</span> <span class="nn">torch.utils.data</span> <span class="kn">import</span> <span class="n">ConcatDataset</span><span class="p">,</span> <span class="n">DataLoader</span>

<span class="kn">from</span> <span class="nn">packnet_sfm.datasets</span> <span class="kn">import</span> <span class="n">KITTIDataset</span><span class="p">,</span> <span class="n">DGPDataset</span><span class="p">,</span> <span class="n">ImageDataset</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.datasets.transforms</span> <span class="kn">import</span> <span class="n">get_transforms</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.utils.depth</span> <span class="kn">import</span> <span class="n">inv2depth</span><span class="p">,</span> <span class="n">post_process_inv_depth</span><span class="p">,</span> <span class="n">compute_depth_metrics</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.utils.horovod</span> <span class="kn">import</span> <span class="n">print0</span><span class="p">,</span> <span class="n">world_size</span><span class="p">,</span> <span class="n">rank</span><span class="p">,</span> <span class="n">on_rank_0</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.utils.image</span> <span class="kn">import</span> <span class="n">flip_lr</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.utils.load</span> <span class="kn">import</span> <span class="n">load_class</span><span class="p">,</span> <span class="n">load_class_args_create</span><span class="p">,</span> \
    <span class="n">load_network</span><span class="p">,</span> <span class="n">filter_args</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.utils.logging</span> <span class="kn">import</span> <span class="n">pcolor</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.utils.reduce</span> <span class="kn">import</span> <span class="n">all_reduce_metrics</span><span class="p">,</span> <span class="n">reduce_dict</span><span class="p">,</span> \
    <span class="n">create_dict</span><span class="p">,</span> <span class="n">average_loss_and_metrics</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.utils.save</span> <span class="kn">import</span> <span class="n">save_depth</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.models.model_utils</span> <span class="kn">import</span> <span class="n">stack_batch</span>


<div class="viewcode-block" id="ModelWrapper"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.ModelWrapper">[docs]</a><span class="k">class</span> <span class="nc">ModelWrapper</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Top-level torch.nn.Module wrapper around a SfmModel (pose+depth networks).</span>
<span class="sd">    Designed to use models with high-level Trainer classes (cf. trainers/).</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Model configuration (cf. configs/default_config.py)</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">config</span><span class="p">,</span> <span class="n">resume</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">logger</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">load_datasets</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>

        <span class="c1"># Store configuration, checkpoint and logger</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">config</span> <span class="o">=</span> <span class="n">config</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span> <span class="o">=</span> <span class="n">logger</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">resume</span> <span class="o">=</span> <span class="n">resume</span>

        <span class="c1"># Set random seed</span>
        <span class="n">set_random_seed</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">arch</span><span class="o">.</span><span class="n">seed</span><span class="p">)</span>

        <span class="c1"># Task metrics</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">metrics_name</span> <span class="o">=</span> <span class="s1">&#39;depth&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">metrics_keys</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;abs_rel&#39;</span><span class="p">,</span> <span class="s1">&#39;sqr_rel&#39;</span><span class="p">,</span> <span class="s1">&#39;rmse&#39;</span><span class="p">,</span> <span class="s1">&#39;rmse_log&#39;</span><span class="p">,</span> <span class="s1">&#39;a1&#39;</span><span class="p">,</span> <span class="s1">&#39;a2&#39;</span><span class="p">,</span> <span class="s1">&#39;a3&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">metrics_modes</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="s1">&#39;_pp&#39;</span><span class="p">,</span> <span class="s1">&#39;_gt&#39;</span><span class="p">,</span> <span class="s1">&#39;_pp_gt&#39;</span><span class="p">)</span>

        <span class="c1"># Model, optimizers, schedulers and datasets are None for now</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">optimizer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scheduler</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">train_dataset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">validation_dataset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">test_dataset</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">current_epoch</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="c1"># Prepare model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">prepare_model</span><span class="p">(</span><span class="n">resume</span><span class="p">)</span>

        <span class="c1"># Prepare datasets</span>
        <span class="k">if</span> <span class="n">load_datasets</span><span class="p">:</span>
            <span class="c1"># Requirements for validation (we only evaluate depth for now)</span>
            <span class="n">validation_requirements</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;gt_depth&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span> <span class="s1">&#39;gt_pose&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">}</span>
            <span class="n">test_requirements</span> <span class="o">=</span> <span class="n">validation_requirements</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">prepare_datasets</span><span class="p">(</span><span class="n">validation_requirements</span><span class="p">,</span> <span class="n">test_requirements</span><span class="p">)</span>

        <span class="c1"># Preparations done</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">prepared</span> <span class="o">=</span> <span class="kc">True</span>

<div class="viewcode-block" id="ModelWrapper.prepare_model"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.ModelWrapper.prepare_model">[docs]</a>    <span class="k">def</span> <span class="nf">prepare_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">resume</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Prepare self.model (incl. loading previous state)&quot;&quot;&quot;</span>
        <span class="n">print0</span><span class="p">(</span><span class="n">pcolor</span><span class="p">(</span><span class="s1">&#39;### Preparing Model&#39;</span><span class="p">,</span> <span class="s1">&#39;green&#39;</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">setup_model</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">prepared</span><span class="p">)</span>
        <span class="c1"># Resume model if available</span>
        <span class="k">if</span> <span class="n">resume</span><span class="p">:</span>
            <span class="n">print0</span><span class="p">(</span><span class="n">pcolor</span><span class="p">(</span><span class="s1">&#39;### Resuming from </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">resume</span><span class="p">[</span><span class="s1">&#39;file&#39;</span><span class="p">]),</span> <span class="s1">&#39;magenta&#39;</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;bold&#39;</span><span class="p">]))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">load_network</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">resume</span><span class="p">[</span><span class="s1">&#39;state_dict&#39;</span><span class="p">],</span> <span class="s1">&#39;model&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="s1">&#39;epoch&#39;</span> <span class="ow">in</span> <span class="n">resume</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">current_epoch</span> <span class="o">=</span> <span class="n">resume</span><span class="p">[</span><span class="s1">&#39;epoch&#39;</span><span class="p">]</span></div>

<div class="viewcode-block" id="ModelWrapper.prepare_datasets"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.ModelWrapper.prepare_datasets">[docs]</a>    <span class="k">def</span> <span class="nf">prepare_datasets</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">validation_requirements</span><span class="p">,</span> <span class="n">test_requirements</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Prepare datasets for training, validation and test.&quot;&quot;&quot;</span>
        <span class="c1"># Prepare datasets</span>
        <span class="n">print0</span><span class="p">(</span><span class="n">pcolor</span><span class="p">(</span><span class="s1">&#39;### Preparing Datasets&#39;</span><span class="p">,</span> <span class="s1">&#39;green&#39;</span><span class="p">))</span>

        <span class="n">augmentation</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">augmentation</span>
        <span class="c1"># Setup train dataset (requirements are given by the model itself)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">train_dataset</span> <span class="o">=</span> <span class="n">setup_dataset</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">train</span><span class="p">,</span> <span class="s1">&#39;train&#39;</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">train_requirements</span><span class="p">,</span> <span class="o">**</span><span class="n">augmentation</span><span class="p">)</span>
        <span class="c1"># Setup validation dataset</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">validation_dataset</span> <span class="o">=</span> <span class="n">setup_dataset</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">validation</span><span class="p">,</span> <span class="s1">&#39;validation&#39;</span><span class="p">,</span>
            <span class="n">validation_requirements</span><span class="p">,</span> <span class="o">**</span><span class="n">augmentation</span><span class="p">)</span>
        <span class="c1"># Setup test dataset</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">test_dataset</span> <span class="o">=</span> <span class="n">setup_dataset</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">test</span><span class="p">,</span> <span class="s1">&#39;test&#39;</span><span class="p">,</span>
            <span class="n">test_requirements</span><span class="p">,</span> <span class="o">**</span><span class="n">augmentation</span><span class="p">)</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">depth_net</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Returns depth network.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">depth_net</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">pose_net</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Returns pose network.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">pose_net</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">logs</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Returns various logs for tracking.&quot;&quot;&quot;</span>
        <span class="n">params</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">optimizer</span><span class="o">.</span><span class="n">param_groups</span><span class="p">:</span>
            <span class="n">params</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">_learning_rate&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">param</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">())]</span> <span class="o">=</span> <span class="n">param</span><span class="p">[</span><span class="s1">&#39;lr&#39;</span><span class="p">]</span>
        <span class="n">params</span><span class="p">[</span><span class="s1">&#39;progress&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">progress</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="o">**</span><span class="n">params</span><span class="p">,</span>
            <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">logs</span><span class="p">,</span>
        <span class="p">}</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">progress</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Returns training progress (current epoch / max. number of epochs)&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">current_epoch</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">arch</span><span class="o">.</span><span class="n">max_epochs</span>

<div class="viewcode-block" id="ModelWrapper.configure_optimizers"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.ModelWrapper.configure_optimizers">[docs]</a>    <span class="k">def</span> <span class="nf">configure_optimizers</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Configure depth and pose optimizers and the corresponding scheduler.&quot;&quot;&quot;</span>

        <span class="n">params</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="c1"># Load optimizer</span>
        <span class="n">optimizer</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">optim</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">optimizer</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="c1"># Depth optimizer</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">depth_net</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">params</span><span class="o">.</span><span class="n">append</span><span class="p">({</span>
                <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;Depth&#39;</span><span class="p">,</span>
                <span class="s1">&#39;params&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">depth_net</span><span class="o">.</span><span class="n">parameters</span><span class="p">(),</span>
                <span class="o">**</span><span class="n">filter_args</span><span class="p">(</span><span class="n">optimizer</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">optimizer</span><span class="o">.</span><span class="n">depth</span><span class="p">)</span>
            <span class="p">})</span>
        <span class="c1"># Pose optimizer</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">pose_net</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">params</span><span class="o">.</span><span class="n">append</span><span class="p">({</span>
                <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;Pose&#39;</span><span class="p">,</span>
                <span class="s1">&#39;params&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">pose_net</span><span class="o">.</span><span class="n">parameters</span><span class="p">(),</span>
                <span class="o">**</span><span class="n">filter_args</span><span class="p">(</span><span class="n">optimizer</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">optimizer</span><span class="o">.</span><span class="n">pose</span><span class="p">)</span>
            <span class="p">})</span>
        <span class="c1"># Create optimizer with parameters</span>
        <span class="n">optimizer</span> <span class="o">=</span> <span class="n">optimizer</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>

        <span class="c1"># Load and initialize scheduler</span>
        <span class="n">scheduler</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">optim</span><span class="o">.</span><span class="n">lr_scheduler</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">scheduler</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="n">scheduler</span> <span class="o">=</span> <span class="n">scheduler</span><span class="p">(</span><span class="n">optimizer</span><span class="p">,</span> <span class="o">**</span><span class="n">filter_args</span><span class="p">(</span><span class="n">scheduler</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">scheduler</span><span class="p">))</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">resume</span><span class="p">:</span>
            <span class="k">if</span> <span class="s1">&#39;optimizer&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">resume</span><span class="p">:</span>
                <span class="n">optimizer</span><span class="o">.</span><span class="n">load_state_dict</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">resume</span><span class="p">[</span><span class="s1">&#39;optimizer&#39;</span><span class="p">])</span>
            <span class="k">if</span> <span class="s1">&#39;scheduler&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">resume</span><span class="p">:</span>
                <span class="n">scheduler</span><span class="o">.</span><span class="n">load_state_dict</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">resume</span><span class="p">[</span><span class="s1">&#39;scheduler&#39;</span><span class="p">])</span>

        <span class="c1"># Create class variables so we can use it internally</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">optimizer</span> <span class="o">=</span> <span class="n">optimizer</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">scheduler</span> <span class="o">=</span> <span class="n">scheduler</span>

        <span class="c1"># Return optimizer and scheduler</span>
        <span class="k">return</span> <span class="n">optimizer</span><span class="p">,</span> <span class="n">scheduler</span></div>

<div class="viewcode-block" id="ModelWrapper.train_dataloader"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.ModelWrapper.train_dataloader">[docs]</a>    <span class="k">def</span> <span class="nf">train_dataloader</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Prepare training dataloader.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">setup_dataloader</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">train_dataset</span><span class="p">,</span>
                                <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">train</span><span class="p">,</span> <span class="s1">&#39;train&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span></div>

<div class="viewcode-block" id="ModelWrapper.val_dataloader"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.ModelWrapper.val_dataloader">[docs]</a>    <span class="k">def</span> <span class="nf">val_dataloader</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Prepare validation dataloader.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">setup_dataloader</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">validation_dataset</span><span class="p">,</span>
                                <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">validation</span><span class="p">,</span> <span class="s1">&#39;validation&#39;</span><span class="p">)</span></div>

<div class="viewcode-block" id="ModelWrapper.test_dataloader"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.ModelWrapper.test_dataloader">[docs]</a>    <span class="k">def</span> <span class="nf">test_dataloader</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Prepare test dataloader.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">setup_dataloader</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">test_dataset</span><span class="p">,</span>
                                <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">test</span><span class="p">,</span> <span class="s1">&#39;test&#39;</span><span class="p">)</span></div>

<div class="viewcode-block" id="ModelWrapper.training_step"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.ModelWrapper.training_step">[docs]</a>    <span class="k">def</span> <span class="nf">training_step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">batch</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Processes a training batch.&quot;&quot;&quot;</span>
        <span class="n">batch</span> <span class="o">=</span> <span class="n">stack_batch</span><span class="p">(</span><span class="n">batch</span><span class="p">)</span>
        <span class="n">output</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">(</span><span class="n">batch</span><span class="p">,</span> <span class="n">progress</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">progress</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="s1">&#39;loss&#39;</span><span class="p">:</span> <span class="n">output</span><span class="p">[</span><span class="s1">&#39;loss&#39;</span><span class="p">],</span>
            <span class="s1">&#39;metrics&#39;</span><span class="p">:</span> <span class="n">output</span><span class="p">[</span><span class="s1">&#39;metrics&#39;</span><span class="p">]</span>
        <span class="p">}</span></div>

<div class="viewcode-block" id="ModelWrapper.validation_step"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.ModelWrapper.validation_step">[docs]</a>    <span class="k">def</span> <span class="nf">validation_step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">batch</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Processes a validation batch.&quot;&quot;&quot;</span>
        <span class="n">output</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">evaluate_depth</span><span class="p">(</span><span class="n">batch</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">log_depth</span><span class="p">(</span><span class="s1">&#39;val&#39;</span><span class="p">,</span> <span class="n">batch</span><span class="p">,</span> <span class="n">output</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span>
                                  <span class="bp">self</span><span class="o">.</span><span class="n">validation_dataset</span><span class="p">,</span> <span class="n">world_size</span><span class="p">(),</span>
                                  <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">validation</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="s1">&#39;idx&#39;</span><span class="p">:</span> <span class="n">batch</span><span class="p">[</span><span class="s1">&#39;idx&#39;</span><span class="p">],</span>
            <span class="o">**</span><span class="n">output</span><span class="p">[</span><span class="s1">&#39;metrics&#39;</span><span class="p">],</span>
        <span class="p">}</span></div>

<div class="viewcode-block" id="ModelWrapper.test_step"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.ModelWrapper.test_step">[docs]</a>    <span class="k">def</span> <span class="nf">test_step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">batch</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Processes a test batch.&quot;&quot;&quot;</span>
        <span class="n">output</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">evaluate_depth</span><span class="p">(</span><span class="n">batch</span><span class="p">)</span>
        <span class="n">save_depth</span><span class="p">(</span><span class="n">batch</span><span class="p">,</span> <span class="n">output</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span>
                   <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">test</span><span class="p">,</span>
                   <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">save</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="s1">&#39;idx&#39;</span><span class="p">:</span> <span class="n">batch</span><span class="p">[</span><span class="s1">&#39;idx&#39;</span><span class="p">],</span>
            <span class="o">**</span><span class="n">output</span><span class="p">[</span><span class="s1">&#39;metrics&#39;</span><span class="p">],</span>
        <span class="p">}</span></div>

<div class="viewcode-block" id="ModelWrapper.training_epoch_end"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.ModelWrapper.training_epoch_end">[docs]</a>    <span class="k">def</span> <span class="nf">training_epoch_end</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">output_batch</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Finishes a training epoch.&quot;&quot;&quot;</span>

        <span class="c1"># Calculate and reduce average loss and metrics per GPU</span>
        <span class="n">loss_and_metrics</span> <span class="o">=</span> <span class="n">average_loss_and_metrics</span><span class="p">(</span><span class="n">output_batch</span><span class="p">,</span> <span class="s1">&#39;avg_train&#39;</span><span class="p">)</span>
        <span class="n">loss_and_metrics</span> <span class="o">=</span> <span class="n">reduce_dict</span><span class="p">(</span><span class="n">loss_and_metrics</span><span class="p">,</span> <span class="n">to_item</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

        <span class="c1"># Log to wandb</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">log_metrics</span><span class="p">({</span>
                <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">logs</span><span class="p">,</span> <span class="o">**</span><span class="n">loss_and_metrics</span><span class="p">,</span>
            <span class="p">})</span>

        <span class="k">return</span> <span class="p">{</span>
            <span class="o">**</span><span class="n">loss_and_metrics</span>
        <span class="p">}</span></div>

<div class="viewcode-block" id="ModelWrapper.validation_epoch_end"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.ModelWrapper.validation_epoch_end">[docs]</a>    <span class="k">def</span> <span class="nf">validation_epoch_end</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">output_data_batch</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Finishes a validation epoch.&quot;&quot;&quot;</span>

        <span class="c1"># Reduce depth metrics</span>
        <span class="n">metrics_data</span> <span class="o">=</span> <span class="n">all_reduce_metrics</span><span class="p">(</span>
            <span class="n">output_data_batch</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">validation_dataset</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">metrics_name</span><span class="p">)</span>

        <span class="c1"># Create depth dictionary</span>
        <span class="n">metrics_dict</span> <span class="o">=</span> <span class="n">create_dict</span><span class="p">(</span>
            <span class="n">metrics_data</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">metrics_keys</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">metrics_modes</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">validation</span><span class="p">)</span>

        <span class="c1"># Print stuff</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">print_metrics</span><span class="p">(</span><span class="n">metrics_data</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">validation</span><span class="p">)</span>

        <span class="c1"># Log to wandb</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">log_metrics</span><span class="p">({</span>
                <span class="o">**</span><span class="n">metrics_dict</span><span class="p">,</span> <span class="s1">&#39;global_step&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">current_epoch</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span>
            <span class="p">})</span>

        <span class="k">return</span> <span class="p">{</span>
            <span class="o">**</span><span class="n">metrics_dict</span>
        <span class="p">}</span></div>

<div class="viewcode-block" id="ModelWrapper.test_epoch_end"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.ModelWrapper.test_epoch_end">[docs]</a>    <span class="k">def</span> <span class="nf">test_epoch_end</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">output_data_batch</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Finishes a test epoch.&quot;&quot;&quot;</span>

        <span class="c1"># Reduce depth metrics</span>
        <span class="n">metrics_data</span> <span class="o">=</span> <span class="n">all_reduce_metrics</span><span class="p">(</span>
            <span class="n">output_data_batch</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">test_dataset</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">metrics_name</span><span class="p">)</span>

        <span class="c1"># Create depth dictionary</span>
        <span class="n">metrics_dict</span> <span class="o">=</span> <span class="n">create_dict</span><span class="p">(</span>
            <span class="n">metrics_data</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">metrics_keys</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">metrics_modes</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">test</span><span class="p">)</span>

        <span class="c1"># Print stuff</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">print_metrics</span><span class="p">(</span><span class="n">metrics_data</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">test</span><span class="p">)</span>

        <span class="k">return</span> <span class="p">{</span>
            <span class="o">**</span><span class="n">metrics_dict</span>
        <span class="p">}</span></div>

<div class="viewcode-block" id="ModelWrapper.forward"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.ModelWrapper.forward">[docs]</a>    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Runs the model and returns the output.&quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">,</span> <span class="s1">&#39;Model not defined&#39;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div>

<div class="viewcode-block" id="ModelWrapper.depth"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.ModelWrapper.depth">[docs]</a>    <span class="k">def</span> <span class="nf">depth</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Runs the pose network and returns the output.&quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">depth_net</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">,</span> <span class="s1">&#39;Depth network not defined&#39;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">depth_net</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div>

<div class="viewcode-block" id="ModelWrapper.pose"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.ModelWrapper.pose">[docs]</a>    <span class="k">def</span> <span class="nf">pose</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Runs the depth network and returns the output.&quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">pose_net</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">,</span> <span class="s1">&#39;Pose network not defined&#39;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">pose_net</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div>

<div class="viewcode-block" id="ModelWrapper.evaluate_depth"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.ModelWrapper.evaluate_depth">[docs]</a>    <span class="k">def</span> <span class="nf">evaluate_depth</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">batch</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Evaluate batch to produce depth metrics.&quot;&quot;&quot;</span>
        <span class="c1"># Get predicted depth</span>
        <span class="n">inv_depths</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">(</span><span class="n">batch</span><span class="p">)[</span><span class="s1">&#39;inv_depths&#39;</span><span class="p">]</span>
        <span class="n">depth</span> <span class="o">=</span> <span class="n">inv2depth</span><span class="p">(</span><span class="n">inv_depths</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="c1"># Post-process predicted depth</span>
        <span class="n">batch</span><span class="p">[</span><span class="s1">&#39;rgb&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">flip_lr</span><span class="p">(</span><span class="n">batch</span><span class="p">[</span><span class="s1">&#39;rgb&#39;</span><span class="p">])</span>
        <span class="n">inv_depths_flipped</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">(</span><span class="n">batch</span><span class="p">)[</span><span class="s1">&#39;inv_depths&#39;</span><span class="p">]</span>
        <span class="n">inv_depth_pp</span> <span class="o">=</span> <span class="n">post_process_inv_depth</span><span class="p">(</span>
            <span class="n">inv_depths</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">inv_depths_flipped</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">method</span><span class="o">=</span><span class="s1">&#39;mean&#39;</span><span class="p">)</span>
        <span class="n">depth_pp</span> <span class="o">=</span> <span class="n">inv2depth</span><span class="p">(</span><span class="n">inv_depth_pp</span><span class="p">)</span>
        <span class="n">batch</span><span class="p">[</span><span class="s1">&#39;rgb&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">flip_lr</span><span class="p">(</span><span class="n">batch</span><span class="p">[</span><span class="s1">&#39;rgb&#39;</span><span class="p">])</span>
        <span class="c1"># Calculate predicted metrics</span>
        <span class="n">metrics</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>
        <span class="k">if</span> <span class="s1">&#39;depth&#39;</span> <span class="ow">in</span> <span class="n">batch</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">mode</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">metrics_modes</span><span class="p">:</span>
                <span class="n">metrics</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">metrics_name</span> <span class="o">+</span> <span class="n">mode</span><span class="p">]</span> <span class="o">=</span> <span class="n">compute_depth_metrics</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">params</span><span class="p">,</span> <span class="n">gt</span><span class="o">=</span><span class="n">batch</span><span class="p">[</span><span class="s1">&#39;depth&#39;</span><span class="p">],</span>
                    <span class="n">pred</span><span class="o">=</span><span class="n">depth_pp</span> <span class="k">if</span> <span class="s1">&#39;pp&#39;</span> <span class="ow">in</span> <span class="n">mode</span> <span class="k">else</span> <span class="n">depth</span><span class="p">,</span>
                    <span class="n">use_gt_scale</span><span class="o">=</span><span class="s1">&#39;gt&#39;</span> <span class="ow">in</span> <span class="n">mode</span><span class="p">)</span>
        <span class="c1"># Return metrics and extra information</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="s1">&#39;metrics&#39;</span><span class="p">:</span> <span class="n">metrics</span><span class="p">,</span>
            <span class="s1">&#39;inv_depth&#39;</span><span class="p">:</span> <span class="n">inv_depth_pp</span>
        <span class="p">}</span></div>

    <span class="nd">@on_rank_0</span>
    <span class="k">def</span> <span class="nf">print_metrics</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">metrics_data</span><span class="p">,</span> <span class="n">dataset</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Print depth metrics on rank 0 if available&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">metrics_data</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span>
            <span class="k">return</span>

        <span class="n">hor_line</span> <span class="o">=</span> <span class="s1">&#39;|</span><span class="si">{:&lt;}</span><span class="s1">|&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">&#39;*&#39;</span> <span class="o">*</span> <span class="mi">93</span><span class="p">)</span>
        <span class="n">met_line</span> <span class="o">=</span> <span class="s1">&#39;| </span><span class="si">{:^14}</span><span class="s1"> | </span><span class="si">{:^8}</span><span class="s1"> | </span><span class="si">{:^8}</span><span class="s1"> | </span><span class="si">{:^8}</span><span class="s1"> | </span><span class="si">{:^8}</span><span class="s1"> | </span><span class="si">{:^8}</span><span class="s1"> | </span><span class="si">{:^8}</span><span class="s1"> | </span><span class="si">{:^8}</span><span class="s1"> |&#39;</span>
        <span class="n">num_line</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{:&lt;14}</span><span class="s1"> | </span><span class="si">{:^8.3f}</span><span class="s1"> | </span><span class="si">{:^8.3f}</span><span class="s1"> | </span><span class="si">{:^8.3f}</span><span class="s1"> | </span><span class="si">{:^8.3f}</span><span class="s1"> | </span><span class="si">{:^8.3f}</span><span class="s1"> | </span><span class="si">{:^8.3f}</span><span class="s1"> | </span><span class="si">{:^8.3f}</span><span class="s1">&#39;</span>

        <span class="k">def</span> <span class="nf">wrap</span><span class="p">(</span><span class="n">string</span><span class="p">):</span>
            <span class="k">return</span> <span class="s1">&#39;| </span><span class="si">{}</span><span class="s1"> |&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">string</span><span class="p">)</span>

        <span class="nb">print</span><span class="p">()</span>
        <span class="nb">print</span><span class="p">()</span>
        <span class="nb">print</span><span class="p">()</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">hor_line</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">optimizer</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">bs</span> <span class="o">=</span> <span class="s1">&#39;E: </span><span class="si">{}</span><span class="s1"> BS: </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">current_epoch</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span>
                                       <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">train</span><span class="o">.</span><span class="n">batch_size</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">bs</span> <span class="o">+=</span> <span class="s1">&#39; - </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="n">lr</span> <span class="o">=</span> <span class="s1">&#39;LR (</span><span class="si">{}</span><span class="s1">):&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">optimizer</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">optimizer</span><span class="o">.</span><span class="n">param_groups</span><span class="p">:</span>
                <span class="n">lr</span> <span class="o">+=</span> <span class="s1">&#39; </span><span class="si">{}</span><span class="s1"> </span><span class="si">{:.2e}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">param</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">],</span> <span class="n">param</span><span class="p">[</span><span class="s1">&#39;lr&#39;</span><span class="p">])</span>
            <span class="n">par_line</span> <span class="o">=</span> <span class="n">wrap</span><span class="p">(</span><span class="n">pcolor</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{:&lt;40}{:&gt;51}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">bs</span><span class="p">,</span> <span class="n">lr</span><span class="p">),</span>
                                   <span class="s1">&#39;green&#39;</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;bold&#39;</span><span class="p">,</span> <span class="s1">&#39;dark&#39;</span><span class="p">]))</span>
            <span class="nb">print</span><span class="p">(</span><span class="n">par_line</span><span class="p">)</span>
            <span class="nb">print</span><span class="p">(</span><span class="n">hor_line</span><span class="p">)</span>

        <span class="nb">print</span><span class="p">(</span><span class="n">met_line</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="o">*</span><span class="p">((</span><span class="s1">&#39;METRIC&#39;</span><span class="p">,)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">metrics_keys</span><span class="p">)))</span>
        <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">metrics</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">metrics_data</span><span class="p">):</span>
            <span class="nb">print</span><span class="p">(</span><span class="n">hor_line</span><span class="p">)</span>
            <span class="n">path_line</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">dataset</span><span class="o">.</span><span class="n">path</span><span class="p">[</span><span class="n">n</span><span class="p">],</span> <span class="n">dataset</span><span class="o">.</span><span class="n">split</span><span class="p">[</span><span class="n">n</span><span class="p">]))</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">dataset</span><span class="o">.</span><span class="n">cameras</span><span class="p">[</span><span class="n">n</span><span class="p">])</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span> <span class="c1"># only allows single cameras</span>
                <span class="n">path_line</span> <span class="o">+=</span> <span class="s1">&#39; (</span><span class="si">{}</span><span class="s1">)&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">dataset</span><span class="o">.</span><span class="n">cameras</span><span class="p">[</span><span class="n">n</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span>
            <span class="nb">print</span><span class="p">(</span><span class="n">wrap</span><span class="p">(</span><span class="n">pcolor</span><span class="p">(</span><span class="s1">&#39;*** </span><span class="si">{:&lt;87}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">path_line</span><span class="p">),</span> <span class="s1">&#39;magenta&#39;</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;bold&#39;</span><span class="p">])))</span>
            <span class="nb">print</span><span class="p">(</span><span class="n">hor_line</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">metric</span> <span class="ow">in</span> <span class="n">metrics</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">metrics_name</span> <span class="ow">in</span> <span class="n">key</span><span class="p">:</span>
                    <span class="nb">print</span><span class="p">(</span><span class="n">wrap</span><span class="p">(</span><span class="n">pcolor</span><span class="p">(</span><span class="n">num_line</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                        <span class="o">*</span><span class="p">((</span><span class="n">key</span><span class="o">.</span><span class="n">upper</span><span class="p">(),)</span> <span class="o">+</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">metric</span><span class="o">.</span><span class="n">tolist</span><span class="p">()))),</span> <span class="s1">&#39;cyan&#39;</span><span class="p">)))</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">hor_line</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="p">:</span>
            <span class="n">run_line</span> <span class="o">=</span> <span class="n">wrap</span><span class="p">(</span><span class="n">pcolor</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{:&lt;60}{:&gt;31}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">wandb</span><span class="o">.</span><span class="n">url</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">wandb</span><span class="o">.</span><span class="n">name</span><span class="p">),</span> <span class="s1">&#39;yellow&#39;</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;dark&#39;</span><span class="p">]))</span>
            <span class="nb">print</span><span class="p">(</span><span class="n">run_line</span><span class="p">)</span>
            <span class="nb">print</span><span class="p">(</span><span class="n">hor_line</span><span class="p">)</span>

        <span class="nb">print</span><span class="p">()</span></div>


<div class="viewcode-block" id="set_random_seed"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.set_random_seed">[docs]</a><span class="k">def</span> <span class="nf">set_random_seed</span><span class="p">(</span><span class="n">seed</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">seed</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span>
        <span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span>
        <span class="n">torch</span><span class="o">.</span><span class="n">manual_seed</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span>
        <span class="n">torch</span><span class="o">.</span><span class="n">cuda</span><span class="o">.</span><span class="n">manual_seed_all</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span></div>


<div class="viewcode-block" id="setup_depth_net"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.setup_depth_net">[docs]</a><span class="k">def</span> <span class="nf">setup_depth_net</span><span class="p">(</span><span class="n">config</span><span class="p">,</span> <span class="n">prepared</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Create a depth network</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Network configuration</span>
<span class="sd">    prepared : bool</span>
<span class="sd">        True if the network has been prepared before</span>
<span class="sd">    kwargs : dict</span>
<span class="sd">        Extra parameters for the network</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    depth_net : nn.Module</span>
<span class="sd">        Create depth network</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">print0</span><span class="p">(</span><span class="n">pcolor</span><span class="p">(</span><span class="s1">&#39;DepthNet: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">config</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="s1">&#39;yellow&#39;</span><span class="p">))</span>
    <span class="n">depth_net</span> <span class="o">=</span> <span class="n">load_class_args_create</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
        <span class="n">paths</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;packnet_sfm.networks.depth&#39;</span><span class="p">,],</span>
        <span class="n">args</span><span class="o">=</span><span class="p">{</span><span class="o">**</span><span class="n">config</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">},</span>
    <span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">prepared</span> <span class="ow">and</span> <span class="n">config</span><span class="o">.</span><span class="n">checkpoint_path</span> <span class="ow">is</span> <span class="ow">not</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
        <span class="n">depth_net</span> <span class="o">=</span> <span class="n">load_network</span><span class="p">(</span><span class="n">depth_net</span><span class="p">,</span> <span class="n">config</span><span class="o">.</span><span class="n">checkpoint_path</span><span class="p">,</span>
                                 <span class="p">[</span><span class="s1">&#39;depth_net&#39;</span><span class="p">,</span> <span class="s1">&#39;disp_network&#39;</span><span class="p">])</span>
    <span class="k">return</span> <span class="n">depth_net</span></div>


<div class="viewcode-block" id="setup_pose_net"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.setup_pose_net">[docs]</a><span class="k">def</span> <span class="nf">setup_pose_net</span><span class="p">(</span><span class="n">config</span><span class="p">,</span> <span class="n">prepared</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Create a pose network</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Network configuration</span>
<span class="sd">    prepared : bool</span>
<span class="sd">        True if the network has been prepared before</span>
<span class="sd">    kwargs : dict</span>
<span class="sd">        Extra parameters for the network</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    pose_net : nn.Module</span>
<span class="sd">        Created pose network</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">print0</span><span class="p">(</span><span class="n">pcolor</span><span class="p">(</span><span class="s1">&#39;PoseNet: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">config</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="s1">&#39;yellow&#39;</span><span class="p">))</span>
    <span class="n">pose_net</span> <span class="o">=</span> <span class="n">load_class_args_create</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
        <span class="n">paths</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;packnet_sfm.networks.pose&#39;</span><span class="p">,],</span>
        <span class="n">args</span><span class="o">=</span><span class="p">{</span><span class="o">**</span><span class="n">config</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">},</span>
    <span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">prepared</span> <span class="ow">and</span> <span class="n">config</span><span class="o">.</span><span class="n">checkpoint_path</span> <span class="ow">is</span> <span class="ow">not</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
        <span class="n">pose_net</span> <span class="o">=</span> <span class="n">load_network</span><span class="p">(</span><span class="n">pose_net</span><span class="p">,</span> <span class="n">config</span><span class="o">.</span><span class="n">checkpoint_path</span><span class="p">,</span>
                                <span class="p">[</span><span class="s1">&#39;pose_net&#39;</span><span class="p">,</span> <span class="s1">&#39;pose_network&#39;</span><span class="p">])</span>
    <span class="k">return</span> <span class="n">pose_net</span></div>


<div class="viewcode-block" id="setup_model"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.setup_model">[docs]</a><span class="k">def</span> <span class="nf">setup_model</span><span class="p">(</span><span class="n">config</span><span class="p">,</span> <span class="n">prepared</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Create a model</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Model configuration (cf. configs/default_config.py)</span>
<span class="sd">    prepared : bool</span>
<span class="sd">        True if the model has been prepared before</span>
<span class="sd">    kwargs : dict</span>
<span class="sd">        Extra parameters for the model</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    model : nn.Module</span>
<span class="sd">        Created model</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">print0</span><span class="p">(</span><span class="n">pcolor</span><span class="p">(</span><span class="s1">&#39;Model: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">config</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="s1">&#39;yellow&#39;</span><span class="p">))</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">load_class</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">paths</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;packnet_sfm.models&#39;</span><span class="p">,])(</span>
        <span class="o">**</span><span class="p">{</span><span class="o">**</span><span class="n">config</span><span class="o">.</span><span class="n">loss</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">})</span>
    <span class="c1"># Add depth network if required</span>
    <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">network_requirements</span><span class="p">[</span><span class="s1">&#39;depth_net&#39;</span><span class="p">]:</span>
        <span class="n">model</span><span class="o">.</span><span class="n">add_depth_net</span><span class="p">(</span><span class="n">setup_depth_net</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">depth_net</span><span class="p">,</span> <span class="n">prepared</span><span class="p">))</span>
    <span class="c1"># Add pose network if required</span>
    <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">network_requirements</span><span class="p">[</span><span class="s1">&#39;pose_net&#39;</span><span class="p">]:</span>
        <span class="n">model</span><span class="o">.</span><span class="n">add_pose_net</span><span class="p">(</span><span class="n">setup_pose_net</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">pose_net</span><span class="p">,</span> <span class="n">prepared</span><span class="p">))</span>
    <span class="c1"># If a checkpoint is provided, load pretrained model</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">prepared</span> <span class="ow">and</span> <span class="n">config</span><span class="o">.</span><span class="n">checkpoint_path</span> <span class="ow">is</span> <span class="ow">not</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">load_network</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">config</span><span class="o">.</span><span class="n">checkpoint_path</span><span class="p">,</span> <span class="s1">&#39;model&#39;</span><span class="p">)</span>
    <span class="c1"># Return model</span>
    <span class="k">return</span> <span class="n">model</span></div>


<div class="viewcode-block" id="setup_dataset"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.setup_dataset">[docs]</a><span class="k">def</span> <span class="nf">setup_dataset</span><span class="p">(</span><span class="n">config</span><span class="p">,</span> <span class="n">mode</span><span class="p">,</span> <span class="n">requirements</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Create a dataset class</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Configuration (cf. configs/default_config.py)</span>
<span class="sd">    mode : str {&#39;train&#39;, &#39;validation&#39;, &#39;test&#39;}</span>
<span class="sd">        Mode from which we want the dataset</span>
<span class="sd">    requirements : dict (string -&gt; bool)</span>
<span class="sd">        Different requirements for dataset loading (gt_depth, gt_pose, etc)</span>
<span class="sd">    kwargs : dict</span>
<span class="sd">        Extra parameters for dataset creation</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    dataset : Dataset</span>
<span class="sd">        Dataset class for that mode</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># If no dataset is given, return None</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">path</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">None</span>

    <span class="n">print0</span><span class="p">(</span><span class="n">pcolor</span><span class="p">(</span><span class="s1">&#39;###### Setup </span><span class="si">%s</span><span class="s1"> datasets&#39;</span> <span class="o">%</span> <span class="n">mode</span><span class="p">,</span> <span class="s1">&#39;red&#39;</span><span class="p">))</span>

    <span class="c1"># Global shared dataset arguments</span>
    <span class="n">dataset_args</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;back_context&#39;</span><span class="p">:</span> <span class="n">config</span><span class="o">.</span><span class="n">back_context</span><span class="p">,</span>
        <span class="s1">&#39;forward_context&#39;</span><span class="p">:</span> <span class="n">config</span><span class="o">.</span><span class="n">forward_context</span><span class="p">,</span>
        <span class="s1">&#39;data_transform&#39;</span><span class="p">:</span> <span class="n">get_transforms</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
    <span class="p">}</span>

    <span class="c1"># Loop over all datasets</span>
    <span class="n">datasets</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">split</span><span class="p">)):</span>
        <span class="n">path_split</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">path</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">config</span><span class="o">.</span><span class="n">split</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>

        <span class="c1"># Individual shared dataset arguments</span>
        <span class="n">dataset_args_i</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;depth_type&#39;</span><span class="p">:</span> <span class="n">config</span><span class="o">.</span><span class="n">depth_type</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">if</span> <span class="n">requirements</span><span class="p">[</span><span class="s1">&#39;gt_depth&#39;</span><span class="p">]</span> <span class="k">else</span> <span class="kc">None</span><span class="p">,</span>
            <span class="s1">&#39;with_pose&#39;</span><span class="p">:</span> <span class="n">requirements</span><span class="p">[</span><span class="s1">&#39;gt_pose&#39;</span><span class="p">],</span>
        <span class="p">}</span>

        <span class="c1"># KITTI dataset</span>
        <span class="k">if</span> <span class="n">config</span><span class="o">.</span><span class="n">dataset</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;KITTI&#39;</span><span class="p">:</span>
            <span class="n">dataset</span> <span class="o">=</span> <span class="n">KITTIDataset</span><span class="p">(</span>
                <span class="n">config</span><span class="o">.</span><span class="n">path</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">path_split</span><span class="p">,</span>
                <span class="o">**</span><span class="n">dataset_args</span><span class="p">,</span> <span class="o">**</span><span class="n">dataset_args_i</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="c1"># DGP dataset</span>
        <span class="k">elif</span> <span class="n">config</span><span class="o">.</span><span class="n">dataset</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;DGP&#39;</span><span class="p">:</span>
            <span class="n">dataset</span> <span class="o">=</span> <span class="n">DGPDataset</span><span class="p">(</span>
                <span class="n">config</span><span class="o">.</span><span class="n">path</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">config</span><span class="o">.</span><span class="n">split</span><span class="p">[</span><span class="n">i</span><span class="p">],</span>
                <span class="o">**</span><span class="n">dataset_args</span><span class="p">,</span> <span class="o">**</span><span class="n">dataset_args_i</span><span class="p">,</span>
                <span class="n">cameras</span><span class="o">=</span><span class="n">config</span><span class="o">.</span><span class="n">cameras</span><span class="p">[</span><span class="n">i</span><span class="p">],</span>
            <span class="p">)</span>
        <span class="c1"># Image dataset</span>
        <span class="k">elif</span> <span class="n">config</span><span class="o">.</span><span class="n">dataset</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;Image&#39;</span><span class="p">:</span>
            <span class="n">dataset</span> <span class="o">=</span> <span class="n">ImageDataset</span><span class="p">(</span>
                <span class="n">config</span><span class="o">.</span><span class="n">path</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">config</span><span class="o">.</span><span class="n">split</span><span class="p">[</span><span class="n">i</span><span class="p">],</span>
                <span class="o">**</span><span class="n">dataset_args</span><span class="p">,</span> <span class="o">**</span><span class="n">dataset_args_i</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Unknown dataset </span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">config</span><span class="o">.</span><span class="n">dataset</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>

        <span class="c1"># Repeat if needed</span>
        <span class="k">if</span> <span class="s1">&#39;repeat&#39;</span> <span class="ow">in</span> <span class="n">config</span> <span class="ow">and</span> <span class="n">config</span><span class="o">.</span><span class="n">repeat</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">dataset</span> <span class="o">=</span> <span class="n">ConcatDataset</span><span class="p">([</span><span class="n">dataset</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">repeat</span><span class="p">[</span><span class="n">i</span><span class="p">])])</span>
        <span class="n">datasets</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dataset</span><span class="p">)</span>

        <span class="c1"># Display dataset information</span>
        <span class="n">bar</span> <span class="o">=</span> <span class="s1">&#39;######### </span><span class="si">{:&gt;7}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">dataset</span><span class="p">))</span>
        <span class="k">if</span> <span class="s1">&#39;repeat&#39;</span> <span class="ow">in</span> <span class="n">config</span><span class="p">:</span>
            <span class="n">bar</span> <span class="o">+=</span> <span class="s1">&#39; (x</span><span class="si">{}</span><span class="s1">)&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">repeat</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
        <span class="n">bar</span> <span class="o">+=</span> <span class="s1">&#39;: </span><span class="si">{:&lt;}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">path_split</span><span class="p">)</span>
        <span class="n">print0</span><span class="p">(</span><span class="n">pcolor</span><span class="p">(</span><span class="n">bar</span><span class="p">,</span> <span class="s1">&#39;yellow&#39;</span><span class="p">))</span>

    <span class="c1"># If training, concatenate all datasets into a single one</span>
    <span class="k">if</span> <span class="n">mode</span> <span class="o">==</span> <span class="s1">&#39;train&#39;</span><span class="p">:</span>
        <span class="n">datasets</span> <span class="o">=</span> <span class="p">[</span><span class="n">ConcatDataset</span><span class="p">(</span><span class="n">datasets</span><span class="p">)]</span>

    <span class="k">return</span> <span class="n">datasets</span></div>


<div class="viewcode-block" id="worker_init_fn"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.worker_init_fn">[docs]</a><span class="k">def</span> <span class="nf">worker_init_fn</span><span class="p">(</span><span class="n">worker_id</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Function to initialize workers&quot;&quot;&quot;</span>
    <span class="n">time_seed</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">(),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
    <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time_seed</span> <span class="o">+</span> <span class="n">worker_id</span><span class="p">)</span></div>


<div class="viewcode-block" id="get_datasampler"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.get_datasampler">[docs]</a><span class="k">def</span> <span class="nf">get_datasampler</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">mode</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Distributed data sampler&quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">torch</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">distributed</span><span class="o">.</span><span class="n">DistributedSampler</span><span class="p">(</span>
        <span class="n">dataset</span><span class="p">,</span> <span class="n">shuffle</span><span class="o">=</span><span class="p">(</span><span class="n">mode</span><span class="o">==</span><span class="s1">&#39;train&#39;</span><span class="p">),</span>
        <span class="n">num_replicas</span><span class="o">=</span><span class="n">world_size</span><span class="p">(),</span> <span class="n">rank</span><span class="o">=</span><span class="n">rank</span><span class="p">())</span></div>


<div class="viewcode-block" id="setup_dataloader"><a class="viewcode-back" href="../../../models/models.Wrapper.html#packnet_sfm.models.model_wrapper.setup_dataloader">[docs]</a><span class="k">def</span> <span class="nf">setup_dataloader</span><span class="p">(</span><span class="n">datasets</span><span class="p">,</span> <span class="n">config</span><span class="p">,</span> <span class="n">mode</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Create a dataloader class</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    datasets : list of Dataset</span>
<span class="sd">        List of datasets from which to create dataloaders</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Model configuration (cf. configs/default_config.py)</span>
<span class="sd">    mode : str {&#39;train&#39;, &#39;validation&#39;, &#39;test&#39;}</span>
<span class="sd">        Mode from which we want the dataloader</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    dataloaders : list of Dataloader</span>
<span class="sd">        List of created dataloaders for each input dataset</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="p">[(</span><span class="n">DataLoader</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span>
                        <span class="n">batch_size</span><span class="o">=</span><span class="n">config</span><span class="o">.</span><span class="n">batch_size</span><span class="p">,</span> <span class="n">shuffle</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                        <span class="n">pin_memory</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">num_workers</span><span class="o">=</span><span class="n">config</span><span class="o">.</span><span class="n">num_workers</span><span class="p">,</span>
                        <span class="n">worker_init_fn</span><span class="o">=</span><span class="n">worker_init_fn</span><span class="p">,</span>
                        <span class="n">sampler</span><span class="o">=</span><span class="n">get_datasampler</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">mode</span><span class="p">))</span>
             <span class="p">)</span> <span class="k">for</span> <span class="n">dataset</span> <span class="ow">in</span> <span class="n">datasets</span><span class="p">]</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, Toyota Research Institute (TRI)

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(false);
      });
  </script>

  
  
    
   

</body>
</html>